package cii_lib_graph;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cii_lib.CCII_ACCESS_INTERF;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GCII_BITSTerminal extends javax.swing.JFrame {
	private static final Logger log = LoggerFactory
			.getLogger(GCII_BITSTerminal.class);
	private JPanel dispPane;
	private JToggleButton toggleRight;
	private JLabel label;
	private JToggleButton toggleLeft;
	private CCII_ACCESS_INTERF access;
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		GCII_BITSTerminal inst = new GCII_BITSTerminal();
		inst.setVisible(true);
	}
	
	public GCII_BITSTerminal() {
		super();
		initGUI();
	}
	
	public JPanel getDefaultPanel() {
		return dispPane;
	}
	
	public void setCiiInterface(CCII_ACCESS_INTERF inter) {
		access=inter;
	}
	
	public CCII_ACCESS_INTERF getCiiInteraface() {
		return access;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				dispPane = new JPanel();
				getContentPane().add(dispPane, BorderLayout.CENTER);
				dispPane.setPreferredSize(new java.awt.Dimension(140, 84));
				dispPane.setLayout(null);
				{
					toggleLeft = new JToggleButton();
					dispPane.add(toggleLeft);
					toggleLeft.setText("0");
//					ButtonModel mod = toggleLeft.getModel();
//					mod.setSelected(true);
					toggleLeft.setBounds(0, 28, 63, 28);
					toggleLeft.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) { log.info("Command: {}", evt.getActionCommand());
							
							checkSelection(evt);
							//TODO add your code for toggleLeft.actionPerformed
						}
					});
				}
				{
					toggleRight = new JToggleButton();
					dispPane.add(toggleRight);
					toggleRight.setText("1");
					toggleRight.setBounds(63, 28, 63, 28);
					toggleRight.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) { log.info("Command: {}", evt.getActionCommand());
							checkSelection(evt);
							//TODO add your code for toggleRight.actionPerformed
						}
					});
				}
				{
					label = new JLabel();
					dispPane.add(label);
					label.setText("Name");
					label.setBounds(0, 0, 126, 28);
					label.setHorizontalAlignment(SwingConstants.CENTER);
					label.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				}
			}
			pack();
			this.setSize(136, 86);
		} catch (Exception e) {
			log.error("", e);
		}
	}
	
	private boolean checkSelection(ActionEvent evt) {
		String act="";
		act=evt.getActionCommand();
		if (act.equals("0")) {
			if (!toggleLeft.isSelected()) {
				toggleLeft.setSelected(true);
				toggleRight.setSelected(false);
				log.debug("returning 0");
				return false;
			}
			toggleRight.setSelected(false);
			log.debug("returning 0");
			return false;
		} else {
			if (!toggleRight.isSelected()) {
				toggleRight.setSelected(true);
				toggleLeft.setSelected(false);
				log.debug("returning 1");
				return true;
			}
			log.debug("returning 1");
			toggleLeft.setSelected(false);
			return true;
		}
	}

}
